package com.sykj.cloud.system.controller;

import com.sykj.cloud.common.core.utils.poi.ExcelUtil;
import com.sykj.cloud.common.core.web.controller.BaseController;
import com.sykj.cloud.common.core.web.domain.AjaxResult;
import com.sykj.cloud.common.log.annotation.Log;
import com.sykj.cloud.common.log.enums.LogUserType;
import com.sykj.cloud.common.security.annotation.PreAuthorize;
import com.sykj.cloud.common.security.utils.SecurityUtils;
import com.sykj.cloud.system.domain.SysMenu;
import com.sykj.cloud.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 系统功能菜单Controller
 *
 * @author mr.will
 * @date 2021-03-17
 */
@RestController
@RequestMapping("/menu")
public class SysMenuController extends BaseController
{
    @Autowired
    private ISysMenuService sysMenuService;


    /**
     * 新增系统功能菜单
     */
    @PreAuthorize(hasPermi = "system:menu:add")
    @Log(title = "系统功能菜单", logUserType = LogUserType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMenu sysMenu)
    {
        return toAjax(sysMenuService.insertSysMenu(sysMenu));
    }

    /**
     * 修改系统功能菜单
     */
    @PreAuthorize(hasPermi = "system:menu:edit")
    @Log(title = "系统功能菜单", logUserType = LogUserType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMenu sysMenu)
    {
        return toAjax(sysMenuService.updateSysMenu(sysMenu));
    }

    /**
     * 删除系统功能菜单
     */
    @PreAuthorize(hasPermi = "system:menu:remove")
    @Log(title = "系统功能菜单", logUserType = LogUserType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String guids)
    {
        return toAjax(sysMenuService.deleteSysMenuById(guids));
    }


    /**
     * 获取系统功能菜单详细信息
     */
    @PreAuthorize(hasPermi = "system:menu:query")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(sysMenuService.getSysMenuById(guid));
    }

    /**
     * 导出系统功能菜单列表
     */
    @PreAuthorize(hasPermi = "system:menu:export")
    @Log(title = "系统功能菜单", logUserType = LogUserType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMenu sysMenu) throws IOException
    {
        List<SysMenu> list = sysMenuService.listSysMenus(sysMenu);
        ExcelUtil<SysMenu> util = new ExcelUtil<SysMenu>(SysMenu.class);
        util.exportExcel(response, list, "menu");
    }


    /**
     * 查询系统功能菜单列表
     */
    @PreAuthorize(hasPermi = "system:menu:list")
    @GetMapping("/list")
    public AjaxResult list(SysMenu sysMenu)
    {
        return AjaxResult.success(sysMenuService.listSysMenus(sysMenu));
    }


    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysMenu menu)
    {
        List<SysMenu> menus = sysMenuService.listSysMenus(menu);
        return AjaxResult.success(sysMenuService.buildMenuTreeSelect(menus));
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public AjaxResult roleMenuTreeselect(@PathVariable("roleId") String roleId)
    {
        String userId = SecurityUtils.getUserIds();
        List<SysMenu> menu = sysMenuService.listMenuByUserId(userId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", sysMenuService.listMenuByRoleId(roleId));
        ajax.put("menu", sysMenuService.buildMenuTreeSelect(menu));
        return ajax;
    }


    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        String userId = SecurityUtils.getUserIds();
        List<SysMenu> menu = sysMenuService.listMenuTreeByUserId(userId);
        return AjaxResult.success(sysMenuService.buildMenu(menu));
    }

}
